import axios from './axios';

export async function textToSpeech(text: string, language: string = 'zh') {
  try {
    const response = await axios.post('/speech', 
      { text, language },
      { responseType: 'arraybuffer' }
    );

    const blob = new Blob([response.data], { type: 'audio/mpeg' });
    const url = URL.createObjectURL(blob);
    const audio = new Audio(url);
    
    await audio.play();

    audio.onended = () => {
      URL.revokeObjectURL(url);
    };

    return audio;
  } catch (error) {
    console.error('Speech synthesis error:', error);
    throw error;
  }
}